//
//  customSlider.m
//  customSlider
//
//  Created by sxiaojian on 14-9-10.
//  Copyright (c) 2014年 sxiaojian. All rights reserved.
//

#import "customSlider.h"
#import <QuartzCore/QuartzCore.h>
#define fix     30
@interface customSlider ()
{
    BOOL _thumbOn;
    BOOL _isPlay;
    // track the current touch state of the slider
    UIImageView *thumbImageView;               // the slide knob
    UIImageView *maxmumTrackImageView;         // slider track image in normal state
    UIImageView *minimumTrackImage;    // slider track image in highlighted state
    NSDate *date;
    NSString *thumbImage;
    NSString *thumbHighLightImage;
    NSString *thumbNextImage;
    NSString *maxTrackImage;
    NSString *minTrackImage;
    int leftCapWidth;
    int topCapHeight;
}
@end

@implementation customSlider


#pragma mark - UIView methods
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;    
}

- (void)layoutSubviews
{

    maxmumTrackImageView.frame = self.bounds;
    minimumTrackImage.frame = self.bounds;

    thumbImageView.center = CGPointMake([self xForValue:_value], CGRectGetMidY(maxmumTrackImageView.frame));

    
    // the track
    [self updateTrackHighlight];
}

- (void)drawRect:(CGRect)rect
{
    
    [self updateTrackHighlight];
}

#pragma mark - Accessor Overriding


- (void)setValue:(float)value
{
    if (value < _minimumValue || value > _maximumValue) {
        return;
    }
    
    _value = value;
    
    thumbImageView.center = CGPointMake([self xForValue:_value], CGRectGetMidY(maxmumTrackImageView.frame));
    [self setNeedsDisplay];
}

#pragma mark - Helpers
- (void)commonInit
{
    _value = 0.f;
    _minimumValue = 0.f;
    _maximumValue = 1.f;
    _continuous = YES;
    _thumbOn = NO;
    self.backgroundColor = [UIColor clearColor];
    

}
-(void)setMaxTrackImageName:(NSString*)MaxTrackImageName MinTrackImageName:(NSString*)MinTrackImageName thumbImageName:(NSString*)thumbImageName LeftCapWidth:(int)LeftCapWidth topCapHeight:(int)TopCapHeight thumbNextImage:(NSString *)thumbNextImageName thumbHighLightImage:(NSString *)thumbHighLightImageName
{
    maxTrackImage = MaxTrackImageName;
    minTrackImage = MinTrackImageName;
    thumbImage = thumbImageName;
    leftCapWidth = LeftCapWidth;
    topCapHeight = TopCapHeight;
    thumbNextImage = thumbNextImageName;
    thumbHighLightImage = thumbHighLightImageName;
    maxmumTrackImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:maxTrackImage] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight]];
    [self addSubview:maxmumTrackImageView];
    minimumTrackImage = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:minTrackImage] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight]];
    [self addSubview:minimumTrackImage];
    
    // thumb knob
    thumbImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:thumbNextImage]];
    [self addSubview:thumbImageView];
    
}
//- (float)xForValue:(float)value
//{
//    return (self.frame.size.width) * (value - _minimumValue) / (_maximumValue - _minimumValue);
//}
//
//- (float)valueForX:(float)x
//{
//    return _minimumValue + x / (self.frame.size.width) * (_maximumValue - _minimumValue);
//}
-(float)xForValue:(float)value
{
    return (self.frame.size.width-fix)* (value - _minimumValue) / (_maximumValue - _minimumValue)+self.frame.origin.x-fix/2;
}
- (float)valueForX:(float)x
{
    return  (_maximumValue - _minimumValue) * (x-self.frame.origin.x+fix/2)/(self.frame.size.width-fix);
}
- (void)updateTrackHighlight
{
    // Create a mask layer and the frame to determine what will be visible in the view.
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    CGFloat thumbMidXInHighlightTrack = CGRectGetMidX([self convertRect:thumbImageView.frame toView:maxmumTrackImageView]);
    CGRect maskRect = CGRectMake(0, 0, thumbMidXInHighlightTrack, maxmumTrackImageView.frame.size.height);
    
    // Create a path and add the rectangle in it.
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, nil, maskRect);
    
    // Set the path to the mask layer.
    [maskLayer setPath:path];
    
    // Release the path since it's not covered by ARC.
    CGPathRelease(path);
    
    // Set the mask of the view.
    minimumTrackImage.layer.mask = maskLayer;
}
-(void)changeByother
{
    _isPlay = !_isPlay;
    if (_isPlay) {
        thumbImageView.image = [UIImage imageNamed:thumbImage];
    } else {
        thumbImageView.image = [UIImage imageNamed:thumbNextImage];;
    }
}
-(void)change
{
    NSTimeInterval time =  [date timeIntervalSinceNow];
    float timeinterval = -time;
    if (timeinterval<0.2) {
        _isPlay = !_isPlay;
        if (_isPlay) {
            thumbImageView.image = [UIImage imageNamed:thumbImage];
        } else {
            thumbImageView.image = [UIImage imageNamed:thumbNextImage];;
        }
        [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
    }else
    {
        if (_isPlay) {
            thumbImageView.image = [UIImage imageNamed:thumbImage];
        } else {
            thumbImageView.image = [UIImage imageNamed:thumbNextImage];;
        }
    }
    
}
#pragma mark - Touch events handling
-(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint touchPoint = [touch locationInView:self];
    if(CGRectContainsPoint(thumbImageView.frame, touchPoint)){
        _thumbOn = YES;
        date = [NSDate date];
    }else {
        _thumbOn = NO;
    }
    return YES;
}

-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    if (_thumbOn) {
        _value = [self valueForX:thumbImageView.center.x];
        [self sendActionsForControlEvents:UIControlEventValueChanged];
        [self change];
    }
    _thumbOn = NO;
    [self setNeedsDisplay];
}

-(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    if(!_thumbOn) return YES;
    CGPoint touchPoint = [touch locationInView:self];
    thumbImageView.image = [UIImage imageNamed:thumbHighLightImage];
    thumbImageView.center = CGPointMake( MIN( MAX( [self xForValue:_minimumValue], touchPoint.x), [self xForValue:_maximumValue]), thumbImageView.center.y);
    
    if (_continuous ) {
        _value = [self valueForX:thumbImageView.center.x];

        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
    
    [self setNeedsDisplay];
    return YES;
}

@end
